package com.ccnf.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MpGenerator {
    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Test
    public void mybatisPlus() {
        // 全局配置
        GlobalConfig gc = new GlobalConfig()
                .setOutputDir("D:\\MPCode\\ccnf-invitation")
                .setFileOverride(true)
                .setActiveRecord(true)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setAuthor("developer")
                // 自定义文件命名，注意 %s 会自动填充实体属性
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setDbType(DbType.MYSQL)
                .setDriverName(dataSourceProperties.getDriverClassName())
                .setUrl(dataSourceProperties.getUrl())
                .setUsername(dataSourceProperties.getUsername())
                .setPassword(dataSourceProperties.getPassword())
                .setTypeConvert(new MySqlTypeConvert() {
                    @Override
                    public DbColumnType processTypeConvert(String fieldType) {
                        System.out.println("转换类型：" + fieldType);
                        // 注意！processTypeConvert存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回
                        return super.processTypeConvert(fieldType);
                    }
                });

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig()
//                .setSuperEntityClass("com.ccnf.ticket.model.entity.SuperEntity")// 自定义实体父类
//                .setSuperEntityColumns("")// 自定义实体，公共字段
//                .setSuperMapperClass("com.ccnf.ticket.model.base.SuperMapper")// 自定义Mapper父类
//                .setSuperServiceClass("")// 自定义 service 父类
//                .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")// 自定义 service 实现类父类
//                .setSuperControllerClass("com.baomidou.demo.TestController")// 自定义 controller 父类
//                .setCapitalMode(true)// 全局大写命名 ORACLE 注意
//                .setTablePrefix("")// 这里可以修改为你的表前缀
                .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                .setInclude("reward_cash");// 需要生成的表列表
//                .setExclude("");// 需要排除生成的表列表

        new AutoGenerator().setDataSource(dataSourceConfig)
                // 选择 freemarker 引擎，默认 Veloctiy
//                .setTemplateEngine(new FreemarkerTemplateEngine())
                .setGlobalConfig(gc)
                .setStrategy(strategyConfig)
                .setPackageInfo(new PackageConfig()
                    .setParent("com.ccnf")
                    .setEntity("model.entity")
                    .setMapper("model.mapper")
                    .setXml("model.mapper.xml")
                    .setService("service")
                    .setServiceImpl("service.impl")
                    .setController("web.controller")
                ).execute();
    }
}